/**  
 * @Title: DataWorkService.java
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-05-06 14:23:02 
 */  
package com.fsxgt.datagrid.component.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.fsxgt.datagrid.component.bo.DataWorkBO;
import com.fsxgt.datagrid.component.bo.DataWorkColumnBO;
import com.fsxgt.datagrid.component.dao.DataWorkColumnMapper;
import com.fsxgt.datagrid.component.dao.DataWorkMapper;
import com.fsxgt.datagrid.datax.tool.DataxSecretUtil;
import com.fsxgt.datagrid.flow.dao.FlowInfoMapper;

import lombok.extern.slf4j.Slf4j;

/**  
 * @ClassName: DataWorkService
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-05-06 14:23:02 
*/
@Slf4j
@Service
public class DataWorkService {
	
	@Autowired
	private FlowInfoMapper flowInfoMapper;
	
	@Autowired
	private DataWorkMapper dataWorkMapper;
	@Autowired
	private DataWorkColumnMapper dataWorkColumnMapper;
	
	@Transactional(rollbackFor = Exception.class)
	public void saveDataWork(DataWorkBO dataWorkBO) {
		
		dataWorkBO.setWorkType("1");
		
		dataWorkBO.setUpdateTime(new Date());
		
		if(dataWorkBO.getJsonText()!=null&&dataWorkBO.getJsonText().trim().length()>0) {
			dataWorkBO.setJsonText(DataxSecretUtil.dataxEnCrypt(dataWorkBO.getJsonText()));
		}
		
		if(StringUtils.isEmpty(dataWorkBO.getId())) {
			dataWorkBO.setId(UUID.randomUUID().toString());
			dataWorkBO.setCreateTime(new Date());
			dataWorkMapper.insert(dataWorkBO);
		}else {
			dataWorkMapper.update(dataWorkBO);
		}
		
		
		for(DataWorkColumnBO column:dataWorkBO.getColumnList()) {
			column.setDataWorkId(dataWorkBO.getId());
		}
		dataWorkColumnMapper.delete(dataWorkBO.getId());
		
		

		for(int i=0;i<dataWorkBO.getColumnList().size();i++) {
			dataWorkBO.getColumnList().get(i).setSeq(i+1);
			
		}

		
		if(dataWorkBO.getColumnList().size()>0) {
			dataWorkColumnMapper.insert(dataWorkBO.getColumnList());

		}
		

		
	}

	@Transactional(rollbackFor = Exception.class)
	public void deleteDataWork(String id) {
		// TODO Auto-generated method stub
		dataWorkMapper.delete(id);
		dataWorkColumnMapper.delete(id);
	}

	/**  
	 * @Title: getSyncOffline
	 * @Description: TODO(描述)
	 * @param bo
	 * @author liuyi
	 * @date 2023-05-08 09:51:14 
	 */  
	public DataWorkBO getDataWork(String id) {
		// TODO Auto-generated method stub
		DataWorkBO bo = dataWorkMapper.get(id);
		if(bo!=null) {
			List<DataWorkColumnBO> list = dataWorkColumnMapper.getList(id);
			bo.setColumnList(list);
		}
		return bo;
	}

	/**  
	 * @Title: findJobDataWorkPage
	 * @Description: TODO(描述)
	 * @param param
	 * @return
	 * @author liuyi
	 * @date 2023-09-11 08:20:51 
	 */  
	public List<DataWorkBO> findJobDataWorkPage(DataWorkBO param) {
		// TODO Auto-generated method stub
		
		
		return dataWorkMapper.find(param);
	}

}
